﻿/* 
<system.data>
    <DbProviderFactories>
        <clear />
        <add name="MySQL Data Provider" 
            invariant="MySql.Data.MySqlClient" 
            description=".Net Framework Data Provider for MySQL" 
            type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.1.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
        <add name="SqlClient Data Provider"
            invariant="System.Data.SqlClient" 
            description=".Net Framework Data Provider for SqlServer" 
            type="System.Data.SqlClient.SqlClientFactory, System.Data, 
            Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
        <add name="Oracle Data Provider for .NET" 
            invariant="Oracle.DataAccess.Client" 
            description=".Net Framework Data Provider for Oracle" 
            type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=10.2.0.100, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </DbProviderFactories>
</system.data>
 */



using System;
using System.Collections.Concurrent;
using System.Data.Common;

namespace Framework.Common.Sql
{
    /// <summary>  
    /// DbProviderFactory工厂类  
    /// </summary>  
    public class ProviderFactory
    {
        #region private field and constructor
        private static readonly ConcurrentDictionary<DbProviderType, string> ProviderInvariantNames = new ConcurrentDictionary<DbProviderType, string>();
        private static readonly ConcurrentDictionary<DbProviderType, DbProviderFactory> DbTypeToFactoies = new ConcurrentDictionary<DbProviderType, DbProviderFactory>();
        private static readonly ConcurrentDictionary<string, DbProviderFactory> ProviderNameToFactoies = new ConcurrentDictionary<string, DbProviderFactory>();
        /// <summary>
        /// 
        /// </summary>
        static ProviderFactory()
        {
            //加载已知的数据库访问类的程序集  
            ProviderInvariantNames[DbProviderType.SqlServer] = "System.Data.SqlClient";
            ProviderInvariantNames[DbProviderType.OleDb] = "System.Data.OleDb";
            ProviderInvariantNames[DbProviderType.ODBC] = "System.Data.ODBC";
            ProviderInvariantNames[DbProviderType.Oracle] = "Oracle.DataAccess.Client";
            ProviderInvariantNames[DbProviderType.MySql] = "MySql.Data.MySqlClient";
            ProviderInvariantNames[DbProviderType.SQLite] = "System.Data.SQLite";
            ProviderInvariantNames[DbProviderType.Firebird] = "FirebirdSql.Data.Firebird";
            ProviderInvariantNames[DbProviderType.PostgreSql] = "Npgsql";
            ProviderInvariantNames[DbProviderType.DB2] = "IBM.Data.DB2.iSeries";
            ProviderInvariantNames[DbProviderType.Informix] = "IBM.Data.Informix";
            ProviderInvariantNames[DbProviderType.SqlServerCe] = "System.Data.SqlServerCe";
        }
        #endregion

        #region private
        /// <summary>  
        /// 获取指定数据库类型对应的程序集名称  
        /// </summary>  
        /// <param name="providerType">数据库类型枚举</param>  
        /// <returns></returns>  
        private static string GetProviderInvariantName(DbProviderType providerType)
        {
            return ProviderInvariantNames[providerType];
        }

        /// <summary>  
        /// 加载指定数据库类型的DbProviderFactory  
        /// </summary>  
        /// <param name="providerType">数据库类型枚举</param>  
        /// <returns></returns>  
        private static DbProviderFactory ImportDbProviderFactory(DbProviderType providerType)
        {
            string providerName = GetProviderInvariantName(providerType);
            return ImportDbProviderFactory(providerName);
        }

        /// <summary>  
        /// 加载指定数据库类型的DbProviderFactory  
        /// </summary>
        /// <param name="providerName"></param>
        /// <returns></returns>  
        private static DbProviderFactory ImportDbProviderFactory(string providerName)
        {
            DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);//从全局程序集中查找  
            if (factory == null)
            {
                throw new Exception("DbProviderFactories.GetFactory(" + providerName + ") cannot be null");
            }
            return factory;
        }
        #endregion

        #region public
        /// <summary>  
        /// 获取指定类型的数据库对应的DbProviderFactory  
        /// </summary>  
        /// <param name="providerType">数据库类型枚举</param>  
        /// <returns></returns>  
        public static DbProviderFactory GetDbProviderFactory(DbProviderType providerType)
        {
            DbProviderFactory factory;
            if (!DbTypeToFactoies.TryGetValue(providerType, out factory)) //如果还没有加载，则加载该DbProviderFactory 
            {
                factory = DbTypeToFactoies.GetOrAdd(providerType, ImportDbProviderFactory(providerType));
            }
            return factory;
        }

        /// <summary>  
        /// 获取指定类型的数据库对应的DbProviderFactory  
        /// </summary>
        /// <param name="providerName"></param>
        /// <returns></returns>  
        public static DbProviderFactory GetDbProviderFactory(string providerName)
        {
            DbProviderFactory factory;
            if (!ProviderNameToFactoies.TryGetValue(providerName, out factory)) //如果还没有加载，则加载该DbProviderFactory 
            {
                factory = ProviderNameToFactoies.GetOrAdd(providerName, ImportDbProviderFactory(providerName));
            }
            return factory;
        }
        #endregion
    }
}
